<?php
namespace app\services;

class CampaignService extends BaseService {
    public $matrix = [
        'id' => 'id',
        'name' => 'name',
        'campaign' => 'label',
        'status' => ['status', '$v==1 && $v="waiting";
                                $v==2 && $v="effective";
                                $v==3 && $v="paused";
                                $v==4 && $v="deleted";
                                $v==5 && $v="overdue";
                                $v==6 && $v="closed";
                                return $v;'],
        'launchTime' =>'dateRange',
        'strategyNum' => 'solutionCount',
        'cost' => 'cost',
        'showCount' => 'shows',
        'clickCount' => 'clicks',
        'clickRate' => 'clickRate',
        'cpm' => 'cpm',
        'cpc' => 'cpc',
        'modifiedDate' => 'last_update',
        'frequency_control' => 'frequency_control',
        'operate' => ['id', 'return "edit, view";'],
    ];

    public function getAllCampaigns() {
        $where = [];
        $aIds = $this->getAIds();
        if (!$aIds) return [];
        $where = " advertiser_id in $aIds and status > 0";
        $campaignWhere = $where;
        if ($this->I('label_id')) {
            $campaignWhere .= ' and status > 0 and label_id='.$this->I('label_id');
        }
        $campaigns = $this->getAll('Campaign', $campaignWhere);
        $labels = $this->getAll('CampaignLabel');
        $solutions = $this->getAll('Solution', $where);
        $solutions = $this->arrayGroup($solutions, 'campaign_id');
        $report = new ReportService();
        $campaignReportData = $report->getCampaignReportData();
        $totalCampaignReportData = $report->getCampaignReportData(true);

        foreach ($campaigns as &$campaign) {
            $campaign['label'] = $this->t('common', 'unknown');
            if ($campaign['label_id']) {
                $campaign['label'] = $labels[$campaign['label_id']]['name'];
            }
            $campaign['dateRange'] = $campaign['start_date'].' - '.$campaign['end_date'];
            $campaign['solutionCount'] = 0;
            if (in_array($campaign['id'], array_keys($solutions))) {
                $campaign['solutionCount'] = count($solutions[$campaign['id']]);
            }
            $campaignReport = ['cost'       => 0.00,
                               'shows'      => 0,
                               'clicks'     => 0,
                               'clickRate'  => 0.00,
                               'cpm'        => 0.00,
                               'cpc'        => 0.00];
            if (in_array($campaign['id'], array_keys($campaignReportData))) 
            {
                $campaignReport = $campaignReportData[$campaign['id']];
            }
            if (in_array($campaign['id'], array_keys($totalCampaignReportData))) 
            {
                $campaignReport['cost'] = $totalCampaignReportData[$campaign['id']]['cost'];
            }
            $campaign = array_merge($campaign, $campaignReport);
        }
        return $this->fieldsMap($this->matrix, $campaigns);
    }

    public function getTotalbudget() {
        $camId = $this->I('id');
        $minTotalBudget = 0.01;
        if (!$camId) {
            return $minTotalBudget;
        }
        $sql = "select sum(s.budget) as totalbuget from {{%solution}} as s 
         where campaign_id = {$camId} ";
        $sum = $this->queryOne($sql);
        if ($sum['totalbuget'])
        $minTotalBudget = $sum['totalbuget'];
        return $minTotalBudget;
    }

    public function getCampaignLabels($campaignId = '') {
        $advertiserId = $this->S('advertiser_id');
        if ($campaignId) { 
            $campaign = $this->M('Campaign', $campaignId);
            $advertiserId = $campaign['advertiser_id'];
        }
        return $this->getAll('CampaignLabel', ['advertiser_id'=>$advertiserId]);
    }
}//end
